package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.ExposureListAdapter;
import com.sx.gongzhong.adapter.TextAdapter;
import com.sx.gongzhong.model.ExposureBean;
import com.sx.gongzhong.pest.ExposureListPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IExposureListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ExposureSearchActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
        , IExposureListView {

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_HotTitle)
    LinearLayout ll_HotTitle;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.ll_refresh)
    LinearLayout ll_refresh;
    @BindView(R.id.et_Search)
    EditText et_Search;
    @BindView(R.id.tv_Cancel)
    TextView tv_Cancel;
    private List<String> tilteList;
    private int currentPage=1;
    private ExposureListPest listPest;
    private List<ExposureBean> exposureBeanList=new ArrayList<>();
    private ExposureListAdapter exposureListAdapter;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_exposure_search);
        ButterKnife.bind(this);
        setGoneTitle();
        initViews();
    }

    private void initViews() {
        listPest=new ExposureListPest(this);
        if (tilteList==null){
            tilteList=new ArrayList<>();
        }
       listView.setVisibility(View.GONE);
        ll_HotTitle.setVisibility(View.VISIBLE);
        TextAdapter textAdapter=new TextAdapter(this,R.layout.item_text,tilteList);
        listView.setAdapter(textAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                et_Search.setText(tilteList.get(position));
            }
        });
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        exposureListAdapter = new ExposureListAdapter(this,R.layout.item_exposure_list,exposureBeanList);
        exposureListAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        recyclerView.setAdapter(exposureListAdapter);
        exposureListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                loadData();
            }
        },recyclerView);
        exposureListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(ExposureSearchActivity.this, ExposureDetailActivity.class)
                        .putExtra("ExposureID",exposureBeanList.get(position).getId()));
            }
        });

        et_Search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0){
                    tv_Cancel.setText("搜索");
                }else {
                    tv_Cancel.setText("取消");
                    ll_refresh.setVisibility(View.GONE);
                    ll_HotTitle.setVisibility(View.VISIBLE);
                }
            }
        });
        tv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("取消".equals(tv_Cancel.getText().toString())){
                    finish();
                }else {
                    BaseUtil.hintKb(ExposureSearchActivity.this);
                    onRefresh();
                }
            }
        });
    }

    private void loadData() {
        String title=et_Search.getText().toString().trim();
        if (!BaseUtil.isEmpty(title)){
            listPest.loadExposureList("",title,currentPage,6);
        }
    }

    @Override
    public void onRefresh() {
        ll_refresh.setVisibility(View.VISIBLE);
        currentPage=1;
        loadData();
    }

    @Override
    public void showError(String message) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void loadExposureList(List<ExposureBean> exposureBean,int total) {
        swipeRefreshLayout.setRefreshing(false);
        if (currentPage==1){
            exposureBeanList.clear();
            exposureListAdapter.setNewData(exposureBeanList);
        }
        if (exposureBean!=null&&exposureBean.size()>0) {
            exposureBeanList.addAll(exposureBean);
        }
        if (exposureBeanList.size()>=total){
            exposureListAdapter.loadMoreEnd();
        }else {
            exposureListAdapter.loadMoreComplete();
        }
        exposureListAdapter.notifyDataSetChanged();
    }
}
